(function () {
    window.Game = function () {
        //设置行和列
        this.row = 20;
        this.col = 12
        //初始化
        this.init()
        //实例方块
        this.block = new Block()
        //渲染下一个方块
        this.nextBlock = new Block()
        //渲染地图
        this.map = new Map()
        this.start()
        //事件监听
        this.bindEvent();
        //分数
        this.score = 0
        //速度
        this.during = 30
    }
    Game.prototype.init = function () {
        var $table = $("<table></table>");
        var $table2 = $("<table></table>");
        $table.addClass("tab1")
        $table2.addClass("tab2")
        for (let i = 0; i < this.row; i++) {
            var $tr = $("<tr></tr>");
            for (let j = 0; j < this.col; j++) {
                var $td = $('<td></td>');
                $td.appendTo($tr);
            }
            $tr.appendTo($table)
        }
        for (let i = 0; i < 4; i++) {
            var $tr = $("<tr></tr>");
            for (let j = 0; j < 4; j++) {
                var $td = $('<td></td>');
                $td.appendTo($tr);
            }
            $tr.appendTo($table2)
        }
        $("div").append($table)
        $("div").append($table2)
    }
    Game.prototype.setColor = function (row, col, num) {
        $(".tab1").find("tr").eq(row).children("td").eq(col).addClass("c" + num)
    }
    Game.prototype.setNextColor = function () {
        for (let i = 0; i < 4; i++) {
            for (let j = 0; j < 4; j++) {
                if (this.nextBlock.code[i][j] != 0) {
                    $(".tab2").find("tr").eq(i).children("td").eq(j).addClass("c" + this.nextBlock.code[i][j])
                }

            }

        }

    }
    //清屏功能
    Game.prototype.clear = function () {
        for (let i = 0; i < this.row; i++) {
            for (let j = 0; j < this.col; j++) {
                $(".tab1").find("tr").eq(i).children("td").eq(j).removeClass();
            }
        }
        for (let i = 0; i < 4; i++) {
            for (let j = 0; j < 4; j++) {
                $(".tab2").find("tr").eq(i).children("td").eq(j).removeClass();
            }
        }
    }
    //事件监听
    Game.prototype.bindEvent = function () {
        var self = this;
        $(document).keydown(function (event) {
            if (event.keyCode == 37) {
                self.block.checkLeft();
            } else if (event.keyCode == 39) {
                self.block.checkRight();
            } else if (event.keyCode == 32) {
                //按空格一键到底
                self.block.checkBlockEnd()
            } else if (event.keyCode == 38) {
                //旋转方块
                self.block.blockRot()
            }
        })
    }
    Game.prototype.start = function () {
        var self = this;
        //帧编号
        this.f = 0;
        this.timer = setInterval(function () {
            self.f++;
            document.getElementById("f").innerHTML = "帧编号：" + self.f;
            //清屏
            self.clear();
            self.block.render()
            //渲染预览方块
            self.setNextColor()
            //渲染地图
            self.map.render(self)
            //方块下落
            self.f % self.during == 0 && self.block.checkDown()
        }, 30)
    }
})()